package com.edu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.demo.entity.Contract;
import com.edu.demo.entity.ProResume;
import com.edu.demo.entity.Project;
import com.edu.demo.entity.Student;
import com.edu.demo.exception.MultipleException;
import com.edu.demo.exception.MyNullPointerException;
import com.edu.demo.mapper.ProResumeMapper;
import com.edu.demo.mapper.ProjectMapper;
import com.edu.demo.mapper.StudentMapper;
import com.edu.demo.pojo.AjaxResult;
import com.edu.demo.service.IProResumeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.demo.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目简历投递 服务实现类
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@Service
public class ProResumeServiceImpl extends ServiceImpl<ProResumeMapper, ProResume> implements IProResumeService {

    @Autowired
    private ProResumeMapper proResumeMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private StudentMapper studentMapper;


    /**
     * 投递一个项目简历
     *
     * @param resume
     * @return
     */
    @Override
    public int insertResume(ProResume resume) throws Exception {
        Student student = SecurityUtils.getStudent();
        if(!Objects.isNull(student)){
            resume.setStuId(student.getId());
            QueryWrapper<ProResume> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stu_id",student.getId());
            queryWrapper.eq("project_id",resume.getProjectId());
            List<ProResume> proResumes = proResumeMapper.selectList(queryWrapper);
            if(proResumes.size()>0){
                throw new MultipleException("重复投递");
            }
            resume.setDeliveryTime(new Date());
            return proResumeMapper.insert(resume);

        }
        throw new IllegalStateException("其他用户不允许投递");
    }

    /**
     * 查看、录用、淘汰简历
     *
     * @param resume
     * @return
     */
    @Override
    public int judgeResume(ProResume resume) {
        int update = proResumeMapper.update(resume, new QueryWrapper<ProResume>()
                .eq("project_id", resume.getProjectId())
                .eq("stu_id", resume.getStuId())
        );
        return update;
    }

    /**
     * 返回项目下的简历
     *
     * @param ajaxResult
     * @param index
     * @param size
     * @param project
     */
    @Override
    public void list(AjaxResult ajaxResult, Integer index, Integer size, Integer project) {
        QueryWrapper<ProResume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",project);
        queryWrapper.orderByDesc("status");
        Project project1 = projectMapper.selectById(project);
        if(Objects.isNull(project1)){
            throw new MyNullPointerException("查不到该项目");
            //return;
        }
        ajaxResult.put("project",project1);
        Page<ProResume> page = new Page<>(index,size);
        IPage<ProResume> resumeIPage = proResumeMapper.selectPage(page, queryWrapper);
        List<ProResume> resumes = resumeIPage.getRecords();
        ajaxResult.put("total",resumeIPage.getTotal());
        if(resumes.isEmpty()){
            ajaxResult.put("resumes",Collections.emptyList());
            return;
        }
        List<Integer> studentIds = resumes.stream().map(resume -> resume.getStuId()).collect(Collectors.toList());

        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.in("id",studentIds);
        List<Student> students = studentMapper.selectList(studentQueryWrapper);
        for (int i = 0; i < resumes.size(); i++) {
            for (int i1 = 0; i1 < students.size(); i1++) {
                Student student = students.get(i1);
                if(resumes.get(i).getStuId() == student.getId()){
                    student.setPassword(null);
                    resumes.get(i).setStudent(student);
                }
            }
        }

        ajaxResult.put("resumes",resumes);

    }

    /**
     * 返回学生信息和项目信息
     *
     * @param pro
     * @param stu
     * @return
     */
    @Override
    public Map<String, Object> resumeInfos(Integer pro, Integer stu) {

        Project project = projectMapper.selectById(pro);
        Student student = studentMapper.selectById(stu);
        if(Objects.isNull(project)||Objects.isNull(student)){
            throw new NullPointerException("没有该项目或学生");
        }
        student.setPassword(null);
        Map<String,Object> res = new HashMap<>();
        res.put("project",project);
        res.put("student",student);
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(ProResume entity) {
        QueryWrapper<ProResume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",entity.getProjectId());
        queryWrapper.eq("stu_id",entity.getStuId());

        int update = proResumeMapper.update(entity, queryWrapper);
        if(update>1) throw new MyNullPointerException("更新了多个简历");
        return update ==1;

    }
}
